layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
    //用户列表
    var tableIns = table.render({
        elem: '#list',
        url : '/admin/sys/menu/search',
        page : true,
        limits : [10,15,20,25],
        limit : 20,
        id : "listTable",
        cols : [[
            {field: 'menuId', title: '菜单id',  align:"center"},
            {field: 'parentId', title: '父菜单Id',  align:"center"},
            {field: 'name', title: '菜单名称',   align:'center'},
            {field: 'createTime',  title: '创建时间', align:'center', templet:function(d){
                return moment(d.createTime).format("YYYY-MM-DD HH:mm:ss");
            }},
            {field: 'status', title: '状态', align:'center',templet:function(d){
                 if(d.status == "1" || d.status == "1"){
                        return "<label style='color: #0f2fff'>启用</label>";
                    }else
                        return "<label style='color: #d32f2f'>不启用</label>";
                    }
                },
            {field: 'type', title: '类型', align:'center',templet:function(d){
                    if(d.type == "0"){
                        return "<label style='color: #c7d310'>目录</label>";
                    }else if(d.type == "1" ){
                        return "<label style='color: #1114ff'>菜单</label>";
                    }else{
                        return "无定义";
                    }
                }},
            {title: '操作', templet:'#listBar',fixed:"right",align:"center"}
        ]]
    });

    $(".search_btn").on("click",function(){
            table.reload("listTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    menuId: $('#menuId').val(),
                    name: $('#name').val(),
                    parentId: $('#parentId').val(),
                    type: $('#type').val(),
                    status: $('#status').val()
                }
            })
    });


    function add(edit) {
        var url = "edit.html";
        var title="新增菜单";
        if (edit) {
            url = "edit.html?id=" + edit.menuId;
            title = "修改菜单";
        }
        var index = layui.layer.open({
            title : title,
            type: 2,
            content: url,
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回列表页', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
    }

    $("#addNews_btn").click(function(){
        add();
    });




    function audit(menuId,status){
        $.get("/admin/sys/menu/updateStatus.do",{menuId:menuId,status:status},function (res) {
            if(res.code==0){
                table.reload("listTable",{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        menuId: $('#menuId').val(),
                        name: $('#name').val(),
                        parentId: $('#parentId').val(),
                        type: $('#type').val(),
                        status: $('#status').val()
                    }
                })
            }else{
                layer.msg("操作失败。");
            }
        });
    };

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            add(data);
        }else if(layEvent === 'pass'){
            audit(data.menuId,1);
        }else if(layEvent === 'notPass') {
            audit(data.menuId,0);
        }
    });
});
